import gugga.animations.TimeLineAnimation;
import gugga.common.EventDescriptor;
import gugga.common.ITask;
import gugga.components.ImageLoader;
import gugga.components.ProgressBar;
import gugga.sequence.ExecuteMethodTask;
import gugga.sequence.MonitoringTask;
import gugga.sequence.ProgressMonitor;
import gugga.sequence.SingleExecutionTask;
import gugga.sequence.TaskManager;
import gugga.sequence.TaskSequence;
import gugga.sequence.WaitingTask;

import siteWithSubSections.CommonSection;

/**
 * @author bony
 */
class siteWithSubSections.WhySection extends CommonSection 
{
	// List of WhySection specific fields here. 
	private var mLoadingAnim: TimeLineAnimation;
	private var mProgressBar: ProgressBar;
	private var mProgressMonitor: ProgressMonitor;
	private var mActivatingTaskMonitor: MonitoringTask;
	private var mTaskManager: TaskManager;
	
	// This is a component used to load external resources.
	private var mLoader: ImageLoader;
			
	public function WhySection() 
	{
		super();
		
		// Get the imageloader, its loading animation and progress bar instances from the scene.
		mLoader = getComponent("Loader");
		mLoadingAnim = getComponent("LoadingAnim");
		
		// Use "getComponentByPath()" to obtain a reference to the progress bar,
		// because the "ProgressBar" movie clip is nested in the "LoadingAnim" movie clip 
		// and we can't get to it using getComponent().  
		mProgressBar = getComponentByPath(["LoadingAnim","ProgressBar"]);
	}
	
	public function initUI ()
	{
		super.initUI();
	
		// Sets the path of the image that will be loaded.
		mLoader.ContentPath = "images/sn_bg_approach.gif";
		
		// Set the loading animation to be looping.
		mLoadingAnim.DoLoop = true; 		
		
		// We also need to define a new type of task which will be used to monitor the progress of the image loading.
		// The progress monitor is a manager that we will use to monitor the loading of the section
		// and the update of the progress bar.
		// Create new progress monitor.
		mProgressMonitor = new ProgressMonitor();
		// Attach our progress bar to a progress monitor manager.
		mProgressMonitor.attachProgressBar(mProgressBar);
		// Add the actual resource which progress will be observed.
		mProgressMonitor.addItem(mLoader);
		
		mProgressMonitor.start();
		
		// Prepare and register section sequences. 
				
		// Initialization sequence		
		// Using the CommonSection initialization sequence. 
				
		// Activating sequence
		this.registerActivatingTask(mLoader);
		
		// Open Sequence
		// Show section and start open animation. 
		var openSequence:TaskSequence = new TaskSequence();
		openSequence = prepareOpeningSequence(); 
		this.registerOpeningTask(openSequence);
		
		// Close sequence
		// Using the CommonSection close sequence. 
	}

	private function prepareOpeningSequence() : TaskSequence
	{
		// Show open animation in the beginning, before the loading animation.  		
		var initialShowOpenAnim:ITask = ExecuteMethodTask.create(mOpenAnimation, mOpenAnimation.show, null);
		
		// Create task that will show current section - 
		// this task will be included in appropriate place in open sequence. 
		var showTask:ITask = ExecuteMethodTask.create(this, this.show, null);
		
		// The loading of the image should be executed once that's why we need to wrap the task manager in a single execution task.
		var sectionContentLoadingTask: SingleExecutionTask = prepareTaskManager(); // Loading sequence
		
		// Open Sequence
		// Show section and start open animation 
		var openSequence:TaskSequence = new TaskSequence();
		openSequence.addTask(initialShowOpenAnim);
		openSequence.addTask(showTask);		
		openSequence.addTask(sectionContentLoadingTask);
		openSequence.addTask(mOpenAnimation);
		
		return openSequence;				
	}
	
	private function prepareTaskManager(): SingleExecutionTask
	{
		// The progress bar should be reset, before starting the loader.
		var resetProgressBarTask:ITask = ExecuteMethodTask.create(mProgressBar, mProgressBar.reset, null);
		
		// Create task that will show the loading animation - 
		// this task will be included in appropriate place in the loading sequence.
		var showLoadingTask:ITask = ExecuteMethodTask.create(mLoadingAnim, mLoadingAnim.show, null);
		
		// Create task that will hide the loading animation - 
		// this task will be included in appropriate place in loading sequence.
		var hideLoadingTask:ITask = ExecuteMethodTask.create(mLoadingAnim, mLoadingAnim.hide, null);
		
		// This is a fictive task used only for keeping the loading animation on the scene for 2 seconds,
		// because we want to make sure the user have enough time to see that something is loading. 
		// Other applications, usually load more resources and loading animation does not need such a fictive task.
		// WaitingTask is task,that dispatch event completed after the set time in seconds - example WaitingTask(2).
		var waitingTask:WaitingTask = new WaitingTask(2);		
		
		// This is the precondition, which is necessary for the "hideLoadingTask" to be executed.
		var loaderPrecondition: EventDescriptor = new EventDescriptor(mLoader, "completed");
		
		
		// Instantiate task manager. Add all tasks in their desired order, with their dependencies.
		mTaskManager = new TaskManager();
		// Reset loading bar.
		mTaskManager.addStartingTask(resetProgressBarTask);
		// Show loading bar. 
		mTaskManager.addStartingTask(showLoadingTask);
		// Make sure that loading animation stays enough so that the user can see it.
		mTaskManager.addStartingTask(waitingTask);		
		// Hide loading animation after the waiting task and the loading of the image has completed.
		mTaskManager.addTaskWithPredecessor(hideLoadingTask, waitingTask);
		mTaskManager.addTaskPrecondition(hideLoadingTask, loaderPrecondition);
		// Set the task that marks the completion of the task manager.
		// This is important because the Open animation task will be waiting for the loading task to complete.
		mTaskManager.setFinalTask(hideLoadingTask);	
			
		// The loading of the image should be executed once that's why we need to wrap the task manager in a single execution task.
		var sectionContentLoadingTask:SingleExecutionTask = new SingleExecutionTask(mTaskManager);
		
		return sectionContentLoadingTask;	
	}	
}